#!/usr/bin/php

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("pro_myttc");

$r = mysql_query("SELECT MAX(ROUND(lat*10000-430000)) AS max_lat, MIN(ROUND(lat*10000-430000)) AS min_lat, MAX(ABS(790000+ROUND(lng*10000))) AS max_lng, MIN(ABS(790000+ROUND(lng*10000))) AS min_lng, COUNT(*) AS count FROM shape_points sp LEFT JOIN shapes s ON sp.shape_id = s.id WHERE finalized = 1");
list ($max_lat, $min_lat, $max_lng, $min_lng, $count) = mysql_fetch_row($r);

$r_shape_points = mysql_query("SELECT ROUND(sp.lat*10000-430000) AS lat, ABS(790000+ROUND(sp.lng*10000)) AS lng, shape_id, noun_id, distance FROM shape_points sp LEFT JOIN shapes s ON sp.shape_id = s.id WHERE finalized = 1 ORDER BY shape_id, sp.id");
$r_schedules = mysql_query("SELECT service_id, ROUND(headway), ROUND(travel_time), ROUND(start_time), ROUND(end_time), shape_id, 0 FROM schedules ns LEFT JOIN shapes s ON ns.shape_id = s.id WHERE finalized = 1 AND (SELECT COUNT(*) FROM shape_points where s.id = shape_points.shape_id) > 0 ORDER BY service_id, shape_id, start_time");
$n = mysql_num_rows($r_schedules);
$fp = fopen("data.c", "w");
$text = "

int max_lat = $max_lat;
int min_lat = $min_lat;
int max_lng = $max_lng;
int min_lng = $min_lng;
int diff_lat = " . abs($max_lat - $min_lat) . ";
int diff_lng = " . abs($max_lng - $min_lng) . ";
int center_lat = " . abs($max_lat + $min_lat) / 2 . ";
int center_lng = " . abs($max_lng + $min_lng) / 2 . ";


struct sSchedule {
  int service_id;
  int headway;
  int travel_time;
  int start_time;
  int end_time;
  int shape_id;
  bool trips_generated;
};

int num_schedules = $n;
extern int num_schedules;

";

$text .= "sSchedule schedules[] = {\n";
while ($row = mysql_fetch_row($r_schedules))
{
  if ($row[0] > 3) { $row[0] -= 3; }
//  for ($i = 0 ; $i <= 9 ; $i++) $row[$i] += 0;
//  for ($i = 2 ; $i <= 7 ; $i++) $row[$i] *= 60;
//  $text .= "{"."$row[0],$row[1],$row[2],$row[3], {{"."$row[4],$row[5]},{"."$row[6],$row[7]}}, {"."$row[8],$row[9]}, $row[10]},\n";
  $text .= "{".implode(',', $row)."},\n";
}
$text = substr($text, 0, -2) . "};\n";

$text .= "

struct SHAPE_POINT {
  int x;
  int y;
  int noun_id;
  double distance;
};

struct SHAPE {
  int size;
  SHAPE_POINT * points;
};

\n";

$max_id = 0;
while ($row = mysql_fetch_row($r_shape_points))
{
  $id = round($row[2]);
  unset($row[2]);
  $row[3] += 0;
  $row[4] += 0;
  $all_points[$id][] = $row;
  if ($id > $max_id) $max_id = $id;
}

fwrite($fp, $text);
for ($shape_id = 0 ; $shape_id <= $max_id ; $shape_id ++)
//foreach ($all_points as $shape_id => $points)
{
  $text = "SHAPE_POINT sp$shape_id [] = {";
  if (!isset($all_points[$shape_id])) $all_points[$shape_id] = array(array(0));
  foreach ($all_points[$shape_id] as $point) $text .= "{".implode(",", $point)."},";
  $text = substr($text, 0, -1) . "};\n";
  fwrite($fp, $text);
}

$text = "\nSHAPE shapes[$num_shapes] = {\n";
for ($shape_id = 0 ; $shape_id <= $max_id ; $shape_id ++)
//foreach ($all_points as $shape_id => $points)
{
  if ($all_points[$shape_id] == array(array(0)))
    $text .= "{0, &sp$shape_id"."[0]},";
  else
    $text .= "{".count($all_points[$shape_id]).", &sp$shape_id"."[0]},";
}
$text = substr($text, 0, -1) . "};
int num_shapes = " . count($all_points) . ";\n\n";
fwrite($fp, $text);


$wday_service_id = array(0,0,0,0,0,0,0);
$r = mysql_query("SELECT sunday, monday, tuesday, wednesday, thursday, friday, saturday, id, name FROM services WHERE id <= 3");
while ($row = mysql_fetch_row($r))
{
  for ($i = 0 ; $i < 7 ; $i++)
    if ($row[$i] != 0)
      $wday_service_id[$i] = $row[7] - 1;
}

fwrite($fp, "int wday_service_id[7] = {" . implode(",", $wday_service_id) . "};\n");






fclose($fp);

?>
